package com.km.sglototo.pms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.km.sglototo.annotation.SysLog;
import com.km.sglototo.pms.entity.PmsProduct;
import com.km.sglototo.pms.service.IPmsProductService;
import com.km.sglototo.sys.entity.SysUser;
import com.km.sglototo.utils.CommonResult;
import com.km.sglototo.utils.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@Slf4j
@RestController
@Api(tags = "PmsProductController", description = "商品管理")
@RequestMapping("/pms/pmsProduct/")
public class PmsProductController {

    @Resource
    IPmsProductService pmsProductService;

    @GetMapping(value = "/list")
    public Object getList(PmsProduct entity,
                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        QueryWrapper<PmsProduct> queryWrapper = new QueryWrapper<PmsProduct>();
        Page p = new Page<PmsProduct>(pageNum, pageSize);
        return new CommonResult().success(pmsProductService.page(p, queryWrapper));

    }

    @PostMapping(value = "/add")
    public Object addPmsProduct(@RequestBody PmsProduct pmsProduct) {
        boolean b = pmsProductService.save(pmsProduct);
        return new CommonResult().success();
    }


    @SysLog(MODULE = "pms", REMARK = "修改商品")
    @ApiOperation("修改商品")
    @PostMapping(value = "/update")
    public Object updatePmsProduct(@RequestBody PmsProduct pmsProduct) {

        boolean b = pmsProductService.updateById(pmsProduct);
        return new CommonResult().success();

    }

    @SysLog(MODULE = "PMS", REMARK = "删除商品")
    @ApiOperation("删除商品")
    @GetMapping(value = "/delete/{id}")
    public Object deleteUser(@ApiParam("用户id") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("用户id");
            }

            if (pmsProductService.removeById(id)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            log.error("删除用户：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }


    @SysLog(MODULE = "PMS", REMARK = "删除商品")
    @ApiOperation("获取单个商品")
    @GetMapping(value = "/{id}")
    public Object getPmsProductById(@ApiParam("商品id") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("商品id");
            }

            PmsProduct pmsProduct = pmsProductService.getById(id);
            if(pmsProduct != null){
                return new CommonResult().success(pmsProduct);
            }

        } catch (Exception e) {
            log.error("删除用户：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }


}
